package org.breathe.datastruct.impl;

import org.breathe.datastruct.RedisBytes;
import org.breathe.datastruct.RedisData;
import org.breathe.internal.Dict;
import org.breathe.internal.SkipList;
import org.breathe.protocal.Resp;
import org.breathe.protocal.decode.BulkString;

import java.util.*;

/**
 * @author: breathe
 * @createTime: 2025-05-16
 */
public class RedisZSet implements RedisData {
    private volatile long timeout = -1;
    private SkipList<String> skipList;
    private Dict<Double, Object> dict;
    public RedisZSet() {
        skipList = new SkipList();
        dict = new Dict<>();
    }
    @Override
    public long timeout() {
        return timeout;
    }

    @Override
    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    @Override
    public List<Resp> convertToResp() {
        List<Resp> result = new ArrayList<>();
        if (dict.size() == 0) {
            return result;
        }
        for (Double score : dict.keySet()) {
            Object member = dict.get(score);
            Resp[] resp = new Resp[2];
            resp[0] = new BulkString(score.toString().getBytes());
            if(member instanceof RedisBytes){
                resp[1] = new BulkString(((RedisBytes) member).getBytes());
            }else{
                resp[1] = new BulkString(member.toString().getBytes());
            }
        }
        return result;
    }

    public boolean add(double score, Object member) {
        if (dict.containsKey(score, member)) {
            return false;
        }
        dict.put(score, member);
        String memberStr;
        if (member instanceof RedisBytes redisBytes) {
            memberStr = redisBytes.getString();
        } else {
            memberStr = member.toString();
        }
        skipList.insert(score, memberStr);
        return true;
    }
    public List<SkipList.SkipListNode<String>> getRankRange(int start, int end) {
        return skipList.getElementByRankRange(start, end);
    }
    public List<SkipList.SkipListNode<String>> getScoreRange(double start, double end) {
        return skipList.getElementByScoreRange(start, end);
    }
    public int size() {
        return dict.size();
    }

    @SuppressWarnings("unchecked")
    public Iterable<? extends Map.Entry<Double, Object>> getAll() {
        Iterable<? extends Map.Entry<RedisBytes, Object>> raw = dict.entrySet();
        List<Map.Entry<Double, Object>> result = new ArrayList<>();
        for (Map.Entry<RedisBytes, Object> entry : raw) {
            RedisBytes key = entry.getKey();
            Object value = entry.getValue();
            result.add(new AbstractMap.SimpleEntry<>(convertToDouble(key), value));
        }
        return result;
    }

    private Double convertToDouble(RedisBytes bytes) {
        try {
            return Double.parseDouble(bytes.getString());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("无法将 RedisBytes 转换为 Double: " + bytes, e);
        }
    }
}
